package com.a16.yuejian.yuejian.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.adapter.CourseAdapter;
import com.a16.yuejian.yuejian.adapter.QuestionAdapter;
import com.a16.yuejian.yuejian.javabean.Question;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.model.CourseModel;
import com.a16.yuejian.yuejian.model.QuestionModel;

import java.util.List;

public class MaQuestionFragment extends Fragment {
    private View view=null;
    private SharedPreferences sp;
    private RecyclerView recyclerview;
    private EditText qt;
    private Button ask;

    Listener<Question> listener = new Listener<Question>() {
        @Override
        public void onResponse(List<Question> t) {
            QuestionAdapter questionAdapter = new QuestionAdapter(getActivity());
            questionAdapter.setList(t);
            recyclerview.setAdapter(questionAdapter);
        }

        @Override
        public void onFail() {

        }
    };

    SuccessListener successListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(getActivity(),"提问成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(getActivity(),"提问失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.fragment_ma_question, container, false);
        initViews();
        QuestionModel questionModel = new QuestionModel();
        questionModel.allquestion(sp.getString("coachid",""),listener);
        ask();
        return view;
    }

    private void initViews() {
        recyclerview = (RecyclerView)view.findViewById(R.id.rv_question);
        recyclerview.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        qt = (EditText)view.findViewById(R.id.qt_question);
        ask = (Button)view.findViewById(R.id.qt_ask);
    }

    private void ask() {
        ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String q=qt.getText().toString();
                if(q!=null&&"".equals(q)){
                    Toast.makeText(getActivity(), "请输入问题",Toast.LENGTH_SHORT).show();
                }else {
                    QuestionModel questionModel = new QuestionModel();
                    questionModel.ask(sp.getString("coachid",""),sp.getString("uid",""),q,successListener);
                }
            }
        });
    }
}
